define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue','template'], function ($, undefined, Backend, Table, Form,Vue,Template) {

    function initSearchVue() {
        var vue = new Vue({
            el:'#search_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                village_list: [],
                province_id : 0,
                city_id : 0,
                county_id : 0,
                village_id : 0,
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                    this.village_list = [];
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                    this.village_list = [];
                },
                county_id:function (val) {
                    this.county_id = val;
                    this.getVillageList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                },
                getVillageList:function(){
                    var that = this;
                    $.get("adminpublic/ajax_village_list", {
                        county_id : that.county_id
                    }, function (res) {
                        that.village_list = res;
                    });
                }
            }
        });
        vue.getProvinceList();
        return vue;
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID'},
                        {field: 'order_sn', title: __('Order sn'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'user_name',title: __('User name'), searchable:false},
                        {field: 'house_property_name',title: __('Address'), searchable:false},
                        {field: 'money', title: __('Money'), searchable:false},
                        {field: 'water_charge', title: __('Water charge'), searchable:false},
                        {field: 'property_charge', title: __('Property charge'), searchable:false},
                        {field: 'pay_time', title: __('Pay time'),
                            formatter: Table.api.formatter.datetime, operate: 'BETWEEN',
                            type: 'datetime', addclass: 'datetimepicker',
                            data: 'data-date-format="YYYY-MM-DD"'
                        },
                        {field: 'village_id', title: __('Village name'),visible:false,searchList: function (column) {
                                return Template('village_search', {});
                            }
                        },
                        {field: 'status', title: __('Status'),formatter:function(value,row,index){
                                value = '' + value;
                                switch (value){
                                    case '0':
                                        return '<a class="btn btn-default btn-xs btn-deal">' + __('Pay wait') + '</a>'
                                    case '1':
                                        return '<a class="btn btn-default btn-xs btn-dealed">' + __('Payed') + '</a>'
                                }
                            }
                            ,searchable:false
                        },
                    ]
                ]
            });
            initSearchVue();
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
    };
    return Controller;
});